package com.aidex.tina.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import com.aidex.common.exception.BizException;
import com.aidex.common.utils.StringUtils;
import com.aidex.tina.constant.ErrorCode;
import com.aidex.tina.constant.TinaCommon;
import com.aidex.tina.domain.BillItem;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.BaseEntity;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.tina.mapper.BillMapper;
import com.aidex.tina.domain.Bill;
import com.aidex.tina.service.BillService;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.tina.mapper.BillItemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.common.exception.CustomException;

/**
 * 采购订单Service业务层处理
 * @author Stanfine
 * @email shanliang@outlook.com
 * @date 2022-06-08
 */
@Service
@Transactional(readOnly = true)
public class BillServiceImpl extends BaseServiceImpl<BillMapper, Bill> implements BillService {

    private static final Logger log = LoggerFactory.getLogger(BillServiceImpl.class);

    @Autowired(required = false)
    private BillItemMapper billItemMapper;

    /**
     * 获取单条数据
     * @param bill 采购订单
     * @return 采购订单
     */
    @Override
    public Bill get(Bill bill) {
        Bill dto = super.get(bill);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 采购订单id
     * @return 采购订单
     */
    @Override
    public Bill get(String id) {
        Bill dto = super.get(id);
        BillItem billItem = new BillItem();
        billItem.setBillId(id);
        dto.setBillItems(billItemMapper.findList(billItem));
        return dto;
    }

    /**
     * 查询采购订单列表
     * @param bill 采购订单
     * @return 采购订单
     */
    @Override
    public List<Bill> findList(Bill bill) {
		List<Bill> billList = super.findList(bill);
        return billList;
    }

    /**
     * 分页查询采购订单列表
     * @param bill 采购订单
     * @return 采购订单
     */
    @Override
    public PageInfo<Bill> findPage(Bill bill) {
		PageInfo<Bill> page = super.findPage(bill);
        return page;
    }

    /**
     * 保存采购订单
     * @param bill
     * @return 结果
     */
    @Override
    @Transactional
    public boolean save(Bill bill) {
        if (StringUtils.isNotBlank(bill.getId())) {
            Bill billExit = get(bill.getId());
            if (!Objects.equals(billExit.getStatus(), TinaCommon.BILL_STATUS_UPDATE)) {
                throw new BizException(ErrorCode.BILL_STATUS_CAN_NOT_UPDATE, "当前采购订单状态不可修改");
            }

            billItemMapper.deleteBillItemByBillId(bill.getId());
        } else {
            bill.setStatus(TinaCommon.BILL_STATUS_UPDATE);
            super.save(bill);
        }

        BigDecimal totalPrice = BigDecimal.ZERO;
        long count = 0;
        for (BillItem billItem : bill.getBillItems()) {
            billItem.setBillId(bill.getId());
            billItem.setTotalPrice(billItem.getPrice().multiply(BigDecimal.valueOf(billItem.getCount())));
            billItem.preInsert();
            billItemMapper.insert(billItem);

            count += billItem.getCount();
            totalPrice = totalPrice.add(billItem.getTotalPrice());
        }
        bill.setCount(count);
        bill.setTotalPrice(totalPrice);
        bill.setRealPrice(totalPrice.add(bill.getShipPrice().subtract(bill.getDiscountPrice())));

        super.save(bill);

        return true;
    }

    /**
     * 删除采购订单信息
     * @param bill
     * @return 结果
     */
    @Override
    @Transactional
    public boolean remove(Bill bill) {
        return super.remove(bill);
    }

    /**
     * 批量删除采购订单
     * @param ids 需要删除的采购订单ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBillByIds(String[] ids) {
        for (String id : ids)
        {
            BillItem billItem = new BillItem();
            billItem.setBillId(id);
            List<BillItem> BillItemList = billItemMapper.findList(billItem);
            if (BillItemList.size() > 0)
            {
                throw new CustomException("子表存在数据,不能删除");
            }
        }
        return mapper.deleteBillByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

}
